package cbbx_sm.livecamera;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class ImagePanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JFrame parent = null;
	private BufferedImage image = null;
	private String status = null;
	
	private BufferedImage[] images = null;
	private String[] statuses = null;
	private boolean pinned = false;
	

	/**
	 * This is the default constructor
	 */
	public ImagePanel() {
		super();
	}
	
	public ImagePanel(JFrame parent) {
		super();
		this.setSize(parent.getWidth(), parent.getHeight());
		this.parent = parent;
	}

	@Override
	public void paint(Graphics g) {
		 
		try{ //Alessio
		
		super.paint(g);
		Graphics2D g2d = (Graphics2D)g;
		
		
		if (images!=null){
			
			int x = 0;
			int y = 0;
			double resizeFactor = 1;
			int titlePixHeight = 33; // empirically ..
			double minWastedSpace = Double.MAX_VALUE;
			for (int rows = 1; rows<=images.length; rows++){
				int cols = (int)Math.ceil((double)images.length/(double)rows);
				
				double lResizeFactor =  Math.min(
						((double)parent.getWidth())/((double)(images[0].getWidth()*cols)),
						((double)(parent.getHeight()-titlePixHeight ))/((double)(images[0].getHeight()*rows))
						);
				double wastedSpace = parent.getWidth()*parent.getHeight() - (double)images[0].getWidth()*(double)lResizeFactor*images[0].getHeight()*lResizeFactor*images.length;
				if (wastedSpace<minWastedSpace){
					resizeFactor = lResizeFactor;
					minWastedSpace = wastedSpace;
				}
				
			}
			
			
			//double resizeFactor = 0.5; 
			int w = (int)Math.floor((double)images[0].getWidth()*resizeFactor);//(int)Math.round(((float)parent.getWidth())*Math.sqrt(1/(float)images.length));
			int h = (int)Math.floor((double)images[0].getHeight()*resizeFactor);//(int)Math.round(((float)parent.getHeight())*Math.sqrt(1/(float)images.length));
			for (int i=0; i<images.length;i++){
				g2d.drawImage(images[i], x,y,w,h, this);
				if (statuses !=null){
					g2d.setFont(new Font("MS Mincho",Font.BOLD,21));
					g2d.setColor(Color.red); 
					g2d.drawString(statuses[i], x+w/3, y+h/3);
				}
				if ((x+2*w)>parent.getWidth()){
					x = 0;
					y += h;
				}else
					x+= w;
			}				
		} else if (image != null) {
			g2d.drawImage(image, 0,0, this);
			if (status !=null){
				g2d.setFont(new Font("MS Mincho",Font.BOLD,21));
				g2d.setColor(Color.red); 
				g2d.drawString(status, image.getWidth()/3, image.getHeight()/3 );
			}
				
		} else {
			g2d.drawString("No image", 10, 20);
		}
		
		}
		catch(Exception e){
			System.out.println("**********************Image Panel Exception");
			//e.printStackTrace();
		}
	}

	public void setImage(BufferedImage image) {
		if (this.image==null){
			parent.setSize(image.getWidth(), image.getHeight());
			this.setSize(image.getWidth(), image.getHeight());
		}
			
		this.image = image;	
		repaint();
	}
	
	public void setImage(BufferedImage image,String status) {
		this.status = status;
		setImage(image);
	}

	public void setImage(BufferedImage[] images, String[] statuses) {
		if (this.images==null && !this.pinned){
			parent.setSize(images[0].getWidth(), images[0].getHeight());
			this.setSize(images[0].getWidth(), images[0].getHeight());
		}

		this.images = images;
		this.statuses = statuses;
		repaint();
	}

	/** Whether the panel is to be pinned to its size and location.
	 * @param b T/F if it is to be pinned
	 */
	public void pin(boolean b) {
		pinned = b;
	}
}
